﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using WordSeg;


namespace RefinePOIParserTrainingCorpus
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Length != 3)
            {
                Console.WriteLine("RefinePOIParserTrainingCorpus [POI modifier lexical dictionary] [Raw POI parser training corpus] [Refined POI parser training corpus]");
                return;
            }

            WordSeg.WordSeg wordseg = new WordSeg.WordSeg();
            //Load lexical dictionary
            wordseg.LoadLexicalDict(args[0], true);

            //Initialize word breaker's token instance
            WordSeg.Tokens tokens;
            tokens = wordseg.CreateTokens(1024);

            StreamReader sr = new StreamReader(args[1]);
            StreamWriter sw = new StreamWriter(args[2]);

            int LineNo = 0;
            while (sr.EndOfStream == false)
            {
                //Read a record line
                string strLine = sr.ReadLine();
                LineNo++;

                try
                {
                    string[] items = strLine.Split();
                    string strOutput = "";

                    foreach (string item in items)
                    {
                        int pos = item.IndexOf('[');
                        string strTerm;
                        string strTag;

                        strTag = item.Substring(pos + 1, item.Length - pos - 1 - 1);
                        strTerm = item.Substring(0, pos);

                        if (strTag == "ORG_MOD" || strTag == "M")
                        {
                            wordseg.Segment(strTerm, tokens, false);
                            for (int i = 0; i < tokens.tokenList.Count; i++)
                            {
                                strOutput = strOutput + tokens.tokenList[i].strTerm + "[ORG_MOD] ";
                            }
                        }
                        else
                        {
                            strOutput = strOutput + strTerm + "[" + strTag + "] ";
                        }
                    }
                    sw.WriteLine(strOutput.Trim());
                }
                catch (System.Exception err)
                {
                    Console.WriteLine("Error Line at {0} : {1}", LineNo, strLine);
                    Console.WriteLine("Message: {0}", err.Message);
                }
            }

            sr.Close();
            sw.Close();
        }
    }
}
